package org.nearby.note;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.nearby.note.bean.user;
import org.nearby.note.dao.userDaoImpl;
import org.nearby.note.dialog.RegDlg;
import org.nearby.note.utils.common;

/*
 * 笔记主窗口
 */
public class login extends JFrame {
	private JPanel topPanel = new JPanel();
	private JLabel tip = new JLabel("登录|注册");
	
	private	JPanel mainPanel = new JPanel();
	private JLabel userLabel = new JLabel("用户名：");
	private JTextField userText = new JTextField("请输入用户名...");
	private JLabel pwdLabel = new JLabel("密    码：");
	private JPasswordField pwdText = new JPasswordField();
	
	private JPanel bottomPanel = new JPanel();
	private JButton loginBtn = new JButton("登录");
	private JButton regBtn = new JButton("注册");
	
	private  static final int win_width  = 350;
	private  static final int win_height = 300;
	
	private userDaoImpl impl = new userDaoImpl();
	
	private JFrame _self = this;
	
	public login() {
		initWin();
		initFillData();
		initBind();
	}

	public void initWin() {
		this.setTitle("登录");
		this.setLayout(null); //绝对定位
		//头部panel
		topPanel.setBounds(0, 30, 350, 30);
		tip.setFont(new Font("仿宋", 1, 14));
		
		topPanel.add(tip);
		//主要panel
		mainPanel.setBounds(0, 50, 350, 100);
		mainPanel.setLayout(null); //绝对定位
		userLabel.setBounds(40, 20, 70, 30);
		userText.setBounds(120, 20, 150, 30);
		pwdLabel.setBounds(40, 70, 70, 30);
		pwdText.setBounds(120, 70, 150, 30);
		mainPanel.add(userLabel);
		mainPanel.add(userText);
		mainPanel.add(pwdLabel);
		mainPanel.add(pwdText);
		//顶部panel
		bottomPanel.setBounds(0, 160, 350, 50);
		bottomPanel.add(loginBtn);
		bottomPanel.add(regBtn);
		
		this.add(topPanel);
		this.add(mainPanel);
		this.add(bottomPanel);

		
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setSize(win_width, win_height);
		HashMap<String, Integer> d= common.getWindowCenterSize(win_width, win_height);
		this.setLocation(d.get("width"), d.get("height"));
		this.setVisible(true);
		this.setResizable(false);
	}

	public void initFillData() {

	}

	public void initBind() {
		userText.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (userText.getText().equals("请输入用户名...")) {
					userText.setText("");
				}
			}
		});

		loginBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				user u = new user(userText.getText(), pwdText.getText());
				int user_id = 0;
				try {
					user_id = impl.checkUser(u);
					switch(user_id){
						case -1:
							JOptionPane.showMessageDialog(getContentPane(), "对不起，你的帐号已经在别处登录了，你不能登录!");
						break;
						case 0:
							JOptionPane.showMessageDialog(getContentPane(), "用户名或密码错误!");
						break;
						
						default:
							impl.recordLoginStatus(user_id); //记录登录状态
							dispose();
							new home();
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		regBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JDialog reg = new RegDlg(_self, "注册", false);
			}
		});
	}

	public static void main(String[] args) {
		new login();
	}
}
